package shuashuami.hb.com.manager;

import android.app.ProgressDialog;
import android.content.Context;

import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.Application;
import shuashuami.hb.com.fragment.agent.HbAHomeFragm;
import shuashuami.hb.com.fragment.agent.HbAShareCodeFragm;
import shuashuami.hb.com.fragment.agent.UserFragm;
import shuashuami.hb.com.fragment.bussiness.HbBHomeFragm;
import shuashuami.hb.com.fragment.bussiness.HbBUserFragm;
import shuashuami.hb.com.fragment.HbCHallFragm;
import shuashuami.hb.com.fragment.HbCHomeFragm;
import shuashuami.hb.com.fragment.HbCUserFragm;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.model.HbMenuInfo;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class HbAppManager {

    public enum LoginType {
        CLICKFRAM,  //刷单
        BUSINESS, //商户
        AGENT       //代理
    }

    private static ProgressDialog progressDialog;

    private int type;

    private List<HbMenuInfo> menuInfos;

    private LoginType loginType;

    public List<HbMenuInfo> getMenuInfos() {
        return menuInfos;
    }

    public void setMenuInfos(List<HbMenuInfo> menuInfos) {
        this.menuInfos = menuInfos;
    }

    public LoginType getLoginType() {
        return loginType;
    }

    public void setLoginType(int loginType) {
        this.type = loginType;
        if (loginType == 1) {
            this.loginType = LoginType.CLICKFRAM;

        } else if (loginType == 2) {

            this.loginType = LoginType.AGENT;
        } else if (loginType == 3) {

            this.loginType = LoginType.BUSINESS;
        }

        changeLoginType(this.loginType);
    }

    public int getTypeValue() {
        return this.type;
    }


    // 当前app的登录人
    public void changeLoginType(LoginType _type) {
        if (menuInfos == null) {
            menuInfos = new ArrayList<>();
        }
        menuInfos.clear();
        loginType = _type;

        if (loginType == LoginType.AGENT) {
            initAgentData();

        } else if (loginType == LoginType.BUSINESS) {
            initBusinessData();

        } else if (loginType == LoginType.CLICKFRAM) {
            initClickData();

        }

    }

    // 首页，任务大厅，我的
    public void initClickData() {

        menuInfos.add(new HbMenuInfo(
                new HbCHomeFragm(),
                R.mipmap.menu_home_normal,
                R.mipmap.menu_home_sel,
                "首页"
        ));

        menuInfos.add(new HbMenuInfo(
                new HbCHallFragm(),
                R.mipmap.menu_misson_normal,
                R.mipmap.menu_misson_sel,
                "任务大厅"
        ));

        menuInfos.add(new HbMenuInfo(
                new HbCUserFragm(),
                R.mipmap.menu_user_normal,
                R.mipmap.menu_user_sel,
                "我的"
        ));
    }

    // 首页和我的
    private void initBusinessData() {
        menuInfos.add(new HbMenuInfo(
                new HbBHomeFragm(),
                R.mipmap.menu_home_normal,
                R.mipmap.menu_home_sel,
                "首页"
        ));


        menuInfos.add(new HbMenuInfo(
                new HbBUserFragm(),
                R.mipmap.menu_user_normal,
                R.mipmap.menu_user_sel,
                "我的"
        ));
    }


    private void initAgentData() {
        menuInfos.add(new HbMenuInfo(
                new HbAHomeFragm(),
                R.mipmap.menu_home_normal,
                R.mipmap.menu_home_sel,
                "首页"
        ));

        menuInfos.add(new HbMenuInfo(
                new HbAShareCodeFragm(),
                R.mipmap.menu_sharecode_normal,
                R.mipmap.menu_sharecode_sel,
                "分享验证码"
        ));

        menuInfos.add(new HbMenuInfo(
                new UserFragm(),
                R.mipmap.menu_user_normal,
                R.mipmap.menu_user_sel,
                "我的"
        ));
    }

    private static HbAppManager appManager;

    public static HbAppManager getInstance() {
        if (appManager == null) {
            appManager = new HbAppManager();
        }
        return appManager;
    }

    private static volatile ClearableCookieJar cookieJar;

    public synchronized static void SetClearableCookieJar(Context context) {
        if (cookieJar == null) {
            Application application = (Application) context.getApplicationContext();
            if (application.getClearableCookieJar() == null) {
                cookieJar =
                        new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
                application.SetClearableCookieJar(cookieJar);
            } else {
                cookieJar = application.getClearableCookieJar();
            }
        }
    }

    public static void clearCookieJar() {
        cookieJar = null;
    }

    public static ClearableCookieJar getClearableCookieJar() {
        return cookieJar;
    }
}
